import 'package:find_friend/common/common_widget.dart';
import 'package:find_friend/const/const.dart';
import 'package:find_friend/network/network.dart';
import 'package:find_friend/pages/im/model/groupmodel.dart';
import 'package:find_friend/pages/login&register/model/shared_user_info.dart';
import 'package:flutter/material.dart';

class MsgModFriendGroupPage extends StatefulWidget {

  final SharedUserInfo userInfo;

  const MsgModFriendGroupPage({Key key, this.userInfo}) : super(key: key);

  @override
  _MsgModFriendGroupPageState createState() => _MsgModFriendGroupPageState();
}

class _MsgModFriendGroupPageState extends State<MsgModFriendGroupPage> {

  String groupName;
  @override
  void initState() {
    super.initState();

    _requestGroup();

    selectid = widget.userInfo.group_id;
  }

  List<GroupModel> list = [];

  int selectid = -1;

  _requestGroup() {
    Networktool.request(API.friendgroup,method: NETWORKHTTPMethod.GET, success: (data){
      if(data != null) {
        IMGroupResponse response = IMGroupResponse.fromJson(data);
        list = response.data;
        if(mounted)setState(() {});
      }
    });
  }

  _requestSetGroup(GroupModel e) {
    Networktool.request(API.friendgroupset, params: {"mobile": widget.userInfo.mobile, "gid": e.id },method: NETWORKHTTPMethod.POST, success: (data){
      if(data != null) {
        setState(() {
          selectid = e.id;
        });
        groupName = e.friendgroup;
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: APP_scaffold_back_color,
      appBar: AppBar(
        title: Text("好友分组"),
        backgroundColor: Colors.white,
        centerTitle: true,
        leading: IconButton(
          onPressed: (){
            Navigator.of(context).pop(groupName);
          },
          icon: Image.asset("assets/fanhui.png"),
        ),
        actions: [
          Image.asset("assets/sousuo.png", width: 30,),
        ],
      ),
      body: Container(
        margin: const EdgeInsets.all(10),
        padding: const EdgeInsets.only(bottom: 15),
        decoration: BoxDecoration(
          color: Colors.white,
          border: Border.all(color:APP_divider_color, width: 0.5),
          borderRadius: BorderRadius.circular(10)
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: list.map((e) => Padding(
              padding: const EdgeInsets.only(top: 15, left: 10, right: 10),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(e.friendgroup),
                  SelectIconWidget(width: 20, isselect: selectid == e.id, onTap: (){
                    _requestSetGroup(e);
                  },)
                ],
              ),
            )).toList(),
        ),
      )
    );
  }
}